#include "report.h"
#include "ui_report.h"
#include "ettadatastruct.h"
#include "globaldef.h"
#include "chartutils.h"
#include "blldataflow.h"
#include "customprinter.h"
#include "udiskexport.h"
#include "userlogger.h"
#include "loading.h"
#include "msgbox.h"

Report::Report(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Report)
{
    ui->setupUi(this);
    bdf = BllDataFlow::getInstance();
    exportMgr = new UdiskExport(this);

    connect(exportMgr, &UdiskExport::done, this, &Report::pdfSaveDone);
    connect(bdf, &BllDataFlow::showReportPageSignal, this, &Report::show);
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &Report::retranslateUiSlot);

    initWidget();
}

Report::~Report()
{
    delete chartExpReport;
    delete ui;
}

void Report::save(const QString &path)
{
    CustomPrinter::exportToPdf(ui->wdPrint->grab(), path);
}

void Report::initWidget()
{
    chartExpReport = new QChart;
    seriesExpReport = new QLineSeries;
    xAxisExpReport = new QValueAxis;
    yAxisExpReport = new QValueAxis;

    xAxisExpReport->setTitleBrush(QBrush(QColor(152, 152, 152, 255)));
    xAxisExpReport->setTitleText(tr("Pulse(piece)"));
    ui->lblYAxis->setText(tr("Voltage(V)"));
    xAxisExpReport->setLabelFormat("%d");
    yAxisExpReport->setLabelFormat("%d");

    chartExpReport->addSeries(seriesExpReport);
    chartExpReport->setMargins(QMargins(0, 0, 0, 0));
    chartExpReport->legend()->setVisible(false);
    chartExpReport->addAxis(xAxisExpReport, Qt::AlignBottom);
    chartExpReport->addAxis(yAxisExpReport, Qt::AlignLeft);
    seriesExpReport->attachAxis(xAxisExpReport);
    seriesExpReport->attachAxis(yAxisExpReport);
    ui->chartExpReport->setChart(chartExpReport);

    if (windowWidth >= 1280)
    {
        this->setProperty("cssSize", "large");
    }
}

void Report::retranslateChart()
{
    xAxisExpReport->setTitleText(tr("Pulse(piece)"));
    ui->lblYAxis->setText(tr("Voltage(V)"));
}

void Report::show(const ExperimentInfo &data)
{
    ui->lblSampleName->setText(data.sampleName);
    ui->lblParamName->setText(data.parameterName);
    ui->lblMaterialType->setText(data.specifications);
    ui->lblMaterialBatch->setText(data.batchNumber);
    ui->lblExpVolt->setText(QString("%1V").arg(data.voltage));
    ui->lblExpPulse->setText(QString("%1μs").arg(data.pulse));
    ui->lblExpRunTotalTime->setText(data.duration);
    ui->lblPrintTime->setText(QDateTime::currentDateTime().toString(FMT_S));
    ui->lblExpStartTime->setText(QString("%1%2").arg(tr("Start time:")).arg(data.datetimeExpStart));
    ui->lblSampleVolume->setText(QString("%1ml").arg(QString::number(data.volume, 'f', 2)));

    ChartUtils::paintChart(data.voltageRmsDatapath, xAxisExpReport, yAxisExpReport, seriesExpReport, -1, data.voltage);

    bdf->changeMainPageIndex(MainPageIndex::REPORT);
}

void Report::retranslateUiSlot()
{
    ui->retranslateUi(this);
    retranslateChart();
}

void Report::on_btnBack_clicked()
{
    bdf->returnLastMainPage();
}

void Report::on_btnPrint_clicked()
{
    ShowPrintDialog(ui->wdPrint, tr("Print Report"));
}

void Report::on_btnExport_clicked()
{
    Loading::showLoading(QColor(0,0,0,200), false, tr("exporting..."));
    UserLogger::log(tr("Export Report"));
    exportMgr->exports(ReportType::EXPERIMENT_REPORT, this);
    ui->btnExport->setEnabled(false);
}

void Report::pdfSaveDone(const QString &info, const QString &path)
{
    UserLogger::log(info);

    MsgBox msgBox;
    msgBox.setTitle(tr("ReportReport"));
    msgBox.setText(info);
    msgBox.setInformativeText(path);
    msgBox.setBtnCancelVisible(false);
    msgBox.exec();

    ui->btnExport->setEnabled(true);
    Loading::hideLoading();
}
